<style lang="less">
  @import './index.less';
  @import '../styles/common.less';
</style>

<template>
  <div class="localMessage-div">
    <Card class="localMessage-card" id="localMessage-card">
      <div class="localMessage-list" v-for="el in data_list" @click="toPages(el)">
        <div class="title" v-bind:class="[el.reading != true ? 'weight' : '']" v-text="el.title"></div>
        <div class="time" v-text="el.time"></div>
        <div class="content" v-text="el.content"></div>
      </div>
    </Card>
  </div>
</template>

<script>
  import Cookies from 'js-cookie';
  import Util from '../../libs/util';
  import Utils from '../../libs/mod.utils';
  import Nim from '../../libs/mod.nim';
  import String from '../../libs/mod.string';
  export default {
    name: 'localMessage_index',
    data () {
      return {
        data_list: [],
      };
    },
    created: function () {
      var _this = this;
      _this.initData();
    },
    mounted () {
      var _this = this;
      _this.$nextTick(function () {
        var height = $('#localMessage-card').height();
        var H = height - 41 - 101 - 41 - 51 - 10;
        $('#mag-card .msg-div').height(H);
      });
    },
    methods: {
      initData: function () {
        var _this = this;
        if (Nim.getSyncDoneStatus() != true) {// IM未登录
          setTimeout(function () {
            _this.initData();
          }, 1000);
          return false;
        }
        var msgs = Nim.getMsgs();
        var chat_list = [];
        var data_list = [];
        var sid = 'push-msg';
        if (msgs) {
          chat_list = msgs[sid] || [];
        }
        // 数组排序
        chat_list.sort(_this.createComprisonFunction("time"));

        for (var i = 0; i < chat_list.length && i < 50; i++) {
          var tem = chat_list[i];
          if (!tem) {
            continue;
          }
          var time = tem.time;
          var reading = tem.reading || false;
          var date_time = Utils.timestampToFormatDatetime(time);
          var contentStr = tem.content || '';
          if (contentStr != '') {
            var contentObj = String.toJson(contentStr);
            var data = contentObj.data;
            var msg_from = data.from;
            var msg_from_id = data.from_id;
            var msg_type = data.type;
            var msg_content = data.content;
            var msg_title = data.title;
            var obj = {
              title: msg_title,
              content: msg_content,
              type: msg_type,
              from: msg_from,
              from_id: msg_from_id,
              time: date_time,
              msg: tem,
              reading: reading,
            };
            data_list.push(obj);
          }
        }
        _this.data_list = data_list;
      },
      toPages: function (el) {
        var _this = this;
        var from = el.from;
        var msg = el.msg;
        msg.reading = true; // 已读
        Nim.pushMsg(msg);
        _this.initData();

        switch (from) {
          case 'task' :// 任务
            _this.$router.push({
              name: 'task_index'
            });
            break;
          case 'journal' :// 日志
            _this.$router.push({
              name: 'logs_index'
            });
            break;
          case 'approval' :// 审批
            localStorage.setItem('approval_type', 'approving');//待我审批的
            _this.$router.push({
              name: 'approval_index'
            });
            break;
          case 'announcement' :// 公告
            _this.$router.push({
              name: 'affiche_index'
            });
            break;
          case 'meeting' :// 会议
            _this.$router.push({
              name: 'meeting'
            });
            break;
          case 'clock' :// 签到
            _this.$router.push({
              name: 'clock_today'
            });
            break;
          case 'retire' :// 退休
            _this.$router.push({
              name: 'user_retired'
            });
            break;
          default :
            break;
        }
      },
      createComprisonFunction: function (propertyName) {
        return function (object1, object2) {
          var value1 = object1[propertyName];
          var value2 = object2[propertyName];
          if (value1 > value2) {
            return -1;
          } else if (value1 < value2) {
            return 1;
          } else {
            return 0;
          }
        }
      },
    }
  };
</script>
